﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using FullScreen;
using MotoCommon;
using System.IO;

namespace Demo02
{
    public partial class FrmMain : Form
    {
        private string _sScanData = @"\Application Data\ScanData.txt";	// 数据文件
        private string _sGoodsData = @"\Application Data\GoodsData.csv";	// 数据文件
        private static FullScreenControler _fse = new FullScreenControler();
        private static List<MyGoods> _goodsList = null;
       
        public static List<MyGoods> GoodsList
        {
            get
            {
                return _goodsList;
            }
        }

        public static FullScreenControler Fse
        {
            get
            {
                return _fse;
            }
        }


        //private static List<MyGoods> _scanGoodsList = null;
        //public static List<MyGoods> ScanGoodsList
        //{
        //    get
        //    {
        //        return _scanGoodsList;
        //    }
        //    set
        //    {
        //        _scanGoodsList = value;
        //    }
        //}
        public FrmMain()
        {
            InitializeComponent();

            _fse.makeFullScreen(this);
            _goodsList = new List<MyGoods>();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            this.Height = 212;
            Location = MyUtility.CenterWindow(this);
           
            MotoReaderAction.turnOnBacklight();
            LoadGoodsData();
        }

        private void FormMain_KeyUp(object sender, KeyEventArgs e)
        {
            //按键 1
            if (e.KeyValue == (char)49)	// 扫描盘点
            {
                FrmScan window = new FrmScan();
                window.ShowDialog();
            }

            //按键 2
            if (e.KeyValue == (char)50)
            {
                FrmScanData window = new FrmScanData();
                window.ShowDialog();
            }

            //按键 3
            if (e.KeyValue == (char)51)
            {
                if (MessageBox.Show("确定要清空所有数据吗?", "注意", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.OK)
                {
                    if (System.IO.File.Exists(_sScanData))
                    {
                        using (StreamWriter sw = new StreamWriter(_sScanData, false))
                        {
                            sw.Write("");
                        }
                    }
                    else
                    {
                        System.IO.File.Create(_sScanData);
                    }

                    MessageBox.Show("数据已清空！");
                }
            }


            //按键 4
            if (e.KeyValue == (char)52)
            {
                FrmGoodsList window = new FrmGoodsList();
                window.ShowDialog();
                
            }

            //按键 5
            if (e.KeyValue == (char)53)
            {
                MotoReaderAction.turnOffBacklight();
                _fse.Unload();
                this.Close();
                Application.Exit();
            }

            else if (e.KeyValue == (char)121)	// 按"F"+"F2"键
            {
                //显示数据列表
                MotoCommon.MotoReaderAction.turnOnBacklight();
            }
            else if (e.KeyValue == (char)114)	// 按"F"+"F2"键
            {
                //显示数据列表
                MotoCommon.MotoReaderAction.turnOffBacklight();
            }
        }

        private void LoadGoodsData()
        {
            if(!File.Exists(_sGoodsData))
            {
                return;
            }
            using (StreamReader sr = new StreamReader(_sGoodsData, System.Text.Encoding.Default))
            {
                String sLine;
                while ((sLine = sr.ReadLine()) != null)
                {
                    List<string> sList = MotoCommon.MotoCommon.splitStr(sLine, ',');
                    if (sList.Count == 4)
                    {
                        try
                        {
                            MyGoods record = new MyGoods();
                            record.BarCode = sList[0];
                            record.GoodsName = sList[1];
                            record.GoodsAmount = int.Parse(sList[2]);
                            record.GoodsDate = sList[3];

                            _goodsList.Add(record);
                        }
                        catch
                        {
                            continue;
                        }
                    }
                }

            }
        }
    }
}